package com.mew.slots.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.mew.slots.core.common.game.vo.AwardLine;
import com.mew.slots.core.common.handler.AbstractBetHandler;
import com.mew.slots.core.common.messge.response.SlotsPlayResponseMessage;
import com.mew.slots.core.common.utils.SlotsGameUtil;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.util.free.FreeUtil;
import com.mew.slots.util.util.GameUtils;

import java.awt.*;
import java.lang.reflect.Method;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Hello world!
 *
 */
public class App 
{
    public static boolean cend = true;
    public static int end = 1000;
    public static String game;
    public static int line;
    public static String s;
    public static String gameType;
    public static int freeType;
    public static int[] args;

    public static int time;
    public static TextArea out;

    public static List<SlotsPlayResponseMessage> resList=new ArrayList<>();
    public static int bonus;
    public static List<Long> others = new ArrayList<>();

    public static ArrayList<int[]> rateList=new ArrayList<>();

    public static void appStart() throws Exception {
        resList.clear();
        GameUtils.initRateList(rateList);
        time=0;
        bonus=0;
        others = new ArrayList<>();
        out.setText("游戏模拟压注中...");
        Class handlerClass=Class.forName("com.mew.slots.core.game."+game+"."+ GameUtils.gameNameFormat(game));
        AbstractBetHandler handler=(AbstractBetHandler) handlerClass.newInstance();
        handler.readRoller();
        StrategyGameVO player = SlotsGameUtil.buildTestStrategyGameVO();
        player.setLineNumber(line);
        long changeWin=0;
        int changeMul=0;
        List<AwardLine> changeAwardLineList=new ArrayList<>();
        while(time<end){
            if("三只小猪-锁定金狼狗免费".equals(gameType)){
                resList.add(Play.szxzLockGold(player,handler));
                time++;
            } else if("美女与野兽-锁定免费".equals(gameType)){
                resList.add(Play.mnyysLock(player,handler));
                time++;
            }else if("猪猪大作战-猪免费".equals(gameType)){
                player.getWinFree().setFreeType(freeType);
                resList.add(Play.zzdzzFree(player,handler));
                time++;
            }else if("快乐小狗-锁定免费".equals(gameType)){
                resList.add(Play.klxgFree(player,handler));
                time++;
            }else if ("全部".equals(gameType)){
                player.setOtherMsg(args);
                SlotsPlayResponseMessage res=handler.play(player,null);
                changeAwardLineList.addAll(Arrays.asList(res.getAwardLines()));
                resList.add(res);
                changeAwardLineList.clear();
                time++;
                Play.doOthers(game,res,others);
            }else{
                player.setOtherMsg(args);
                if("免费".equals(gameType)){
                    player.getWinFree().setFrees(1);
                    player.getWinFree().setFreeNumber(1);
                    player.getWinFree().setFreeType(freeType);
                    Method[] methods = FreeUtil.class.getMethods();
                    for (Method method : methods) {
                        if(game.equals(method.getName())){
                            Object obj = method.invoke(null);
                            player.getWinFree().setJson(JSON.toJSONString(obj));
                        }
                    }
                }
                SlotsPlayResponseMessage res=handler.play(player,null);
                changeWin+=res.getWinCoin();
                changeMul+=res.getTotalMultiple();
                changeAwardLineList.addAll(Arrays.asList(res.getAwardLines()));
                if(player.getWinFree().getBounsChange()==0){
                    res.setWinCoin(changeWin);
                    res.setAwardLines(changeAwardLineList.toArray(new AwardLine[]{}));
                    res.setTotalMultiple(changeMul);
                    resList.add(res);
                    if(player.getWinFree().getBounsNumber()>0){
                        bonus++;
                    }
                    player=SlotsGameUtil.buildTestStrategyGameVO();
                    player.setLineNumber(line);
                    changeWin=0;
                    changeMul=0;
                    changeAwardLineList.clear();
                    time++;
                }
            }
        }
        out.setText("结果计算中...");
        pushText();
    }


    private static void pushText(){
        //派彩
        long allwin=resList.stream().mapToLong(SlotsPlayResponseMessage::getWinCoin).sum();
        float allMultiple=resList.stream().mapToInt(SlotsPlayResponseMessage::getTotalMultiple).sum();
        //返奖率 派彩/下注
        float pc=(allwin+0.00f)/(1000L*line*time);
        //免费次数
        long mfcs=resList.stream().filter(res->res.getFree()>0).count();
        long zjcs=resList.stream().filter(res->res.getWinCoin()>0).count();
        Map<String,Long[]> lineCountMap=new HashMap();
        List<Integer> coins=new ArrayList<>();
        //连线统计
        resList.stream().forEach(res->{
            for (int icon : res.getIcons()) {
                coins.add(icon);
            }
            Arrays.stream(res.getAwardLines()).forEach(line->{
                String key=line.getType()+"-"+line.getNum();
                if(lineCountMap.containsKey(key)){
                    lineCountMap.get(key)[0]++;
                    lineCountMap.get(key)[1]+=line.getWin();
                }else{
                    lineCountMap.put(key,new Long[]{1L,line.getWin()});
                }
            });
        });
        //倍数统计
        resList.stream().forEach(res->{
            double rate=res.winCoin/1000.0d/line;
            for (int[] r : rateList) {
                if(rate>r[0]&&rate<=r[1]){
                    r[2]++;
                    break;
                }
            }
        });
        StringBuffer sbf=new StringBuffer();
        sbf.append("总  赢:").append(allwin).append("\n")
         .append("总  压:").append(1000L*line*time).append("\n")
         .append("免费次数：").append(mfcs).append("\n")
         .append("特殊次数:").append(bonus).append("\n")
         .append("返奖率:").append(pc).append("\n")
         .append("免费触发率:").append((mfcs+0.0f)/time).append("\n")
         .append("特殊触发率:").append((bonus+0.0f)/time).append("\n")
         .append("中奖率:").append((zjcs+0.0f)/time).append("\n")
         .append("其他:").append(StrUtil.join(",",others)).append("\n");
        if(s!=null&&!"".equals(s)){
            int ts=Integer.parseInt(s);
            long scount=coins.stream().filter(i->i==ts).count();
            sbf.append("特殊图标出现次数:").append(scount).append("\n");
        }
        sbf.append("\n").append("\n倍数:\n");
        rateList.stream().forEach(r->{
            sbf.append(r[0]).append("-").append(r[1]).append("   ").append(r[2]).append("\n");
        });
        sbf.append("\n").append("\n连线:\n");
        List<String> list = lineCountMap.keySet().stream().collect(Collectors.toList());
        list.sort((a1,a2)->{
            Integer type1=Integer.parseInt(a1.split("-")[0]);
            Integer type2=Integer.parseInt(a2.split("-")[0]);
            int ct=type1.compareTo(type2);
            if(ct!=0){
                return ct;
            }
            Integer num1=Integer.parseInt(a1.split("-")[1]);
            Integer num2=Integer.parseInt(a2.split("-")[1]);
            int cn=num1.compareTo(num2);
            return cn;
        });
        for (String s1 : list) {
            sbf.append(s1).append(" : ").append(lineCountMap.get(s1)[0]).append("   ").append(lineCountMap.get(s1)[1]).append("\n");
        }
        out.setText(sbf.toString());
    }

}
